Named routes topic

Instead of navigating to a route based on the URL, a GoRoute can be given a unique name. To configure a named route, use the name parameter:

GoRoute(
   name: 'song',
   path: 'songs/:songId',
   builder: /* ... */,
 ),

To navigate to a route using its name, call goNamed:

TextButton(
  onPressed: () {
    context.goNamed('song', pathParameters: {'songId': 123});
  },
  child: const Text('Go to song 2'),
),

Alternatively, you can look up the location for a name using namedLocation:

TextButton(
  onPressed: () {
    final String location = context.namedLocation('song', pathParameters: {'songId': 123});
    context.go(location);
  },
  child: const Text('Go to song 2'),
),

To learn more about navigation, see the Navigation topic.

Redirecting to a named route

To redirect to a named route, use the namedLocation API:

redirect: (BuildContext context, GoRouterState state) {
  if (AuthState.of(context).isSignedIn) {
    return context.namedLocation('signIn');
  } else {
    return null;
  }   
},

To learn more about redirection, see the Redirection topic.

Classes

GoRoute Get started Configuration Redirection Transition animations Named routes
A route that is displayed visually above the matching parent route using the Navigator.
GoRoute Get started Configuration Redirection Transition animations Named routes
A route that is displayed visually above the matching parent route using the Navigator.
GoRouter Get started Upgrading Configuration Navigation Redirection Web Deep linking Named routes Error handling
The route configuration for the app.
GoRouter Get started Upgrading Configuration Navigation Redirection Web Deep linking Named routes Error handling
The route configuration for the app.